package src.com.atguigu06.project.team.service;

import src.com.atguigu06.project.team.domain.Architcet;
import src.com.atguigu06.project.team.domain.Designer;
import src.com.atguigu06.project.team.domain.Employee;
import src.com.atguigu06.project.team.domain.Programmer;

/**
 * ClassName: TeamService
 * Package: src.com.atguigu06.project.team.service
 * Description:
 *
 * @Author BuTang
 * @Create 2023/5/10 22:07
 * @Version 1.0
 */
public class TeamService {
    private static int counter =1;
    private final int MAX_MEMNER = 5;
    private Programmer[] team = new Programmer[MAX_MEMNER];
    private int total = 0;//团队实际人数

    /**
     * 获取团队成员（数组长度为实际的长度）
     * @return
     */
    public Programmer[] getTeam() {
        Programmer[] team = new Programmer[total];
        for (int i = 0; i < total; i++) {
            team[i]= this.team[i] ;
        }
        return team;
    }

    /**
     * 添加成员
     * @param e
     * @throws Exception
     */
    public void addMember(Employee e)throws TeamException{

        if (total >= MAX_MEMNER) {
            throw new TeamException("团队成员已满");
        }
        if (!(e instanceof Programmer)) {
            throw new TeamException("输入的人员不是开发人员");
        }
        Programmer p = (Programmer) e;
        switch (p.getStatus()){
            case BUSY : throw  new TeamException("该成员已在团队");
            case VOCATION:throw new TeamException("该成员在休假");
        }
        if (isExist(p)){
            throw new TeamException("该成员已在团队中");
        }
/**
 * 比较团队内部的人员组成是否合适
 */
        int numArc = 0, numDes = 0,numPro = 0;
        for (int i = 0; i < total; i++) {

            if ( team[i] instanceof Architcet)  numArc ++;
            else if ( team[i] instanceof Designer) numDes++;
            else if (team[i] instanceof Programmer) numPro++;
        }
        if (p instanceof Architcet){
            if (numArc>= 1 )throw new TeamException("团队只能有一个架构师");
        }else if (p instanceof Designer){
            if (numDes >=2 )throw new TeamException("团队最多只能有两个设计师");
        }else  if (p instanceof Programmer){
            if (numPro>=3)throw new TeamException("团队只能由三个程序员");
        }


        p.setStatus(Status.BUSY);
        p.setMemberld(counter++);
        team[total]= p;
        total++;

    }
    public void deleteMember(int idex)throws TeamException{
        System.out.println("请输入要删除员工的TID");
        if (idex > total && idex<1) {
            throw new TeamException("找不到该成员");
        }else{
        for (int i = 0; i < total; i++) {
            if (idex == team[i].getMemberId() ) {
                team[i].setStatus(Status.FREE);
                for (int j=i; j < total-1 ; j++)
                    team[j] = team[j+1];
            }

        }total--;
    }
    }
    public boolean isExist(Programmer e) throws TeamException {
        for (int i = 0; i < total; i++) {
            if (team[i].getId() == e.getId()){
                return true;
            }
        }
        return false;
    }


}
